<template>
  <div>
    <el-dialog
      :title="!positionForm.id ? '新增岗位' : '编辑岗位'"
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      width="600px">
      <el-form ref="positionForm" :model="positionForm" label-width="80px">
        <el-form-item label="岗位名称" prop="jobName"
        :rules="[{ required: true, message: '请输入岗位名称', trigger: 'blur' }]">
          <el-input v-model="positionForm.jobName" :disabled="!canAddOrEditPosition"></el-input>
        </el-form-item>
        <el-form-item label="招聘人数" prop="jobNum"
        :rules="[{ required: true, message: '请输入招聘人数', trigger: 'blur' }]">
          <el-input type="number" min="0" step="1" v-model="positionForm.jobNum" @wheel.native.prevent="stopScrollFun($event)" :disabled="!canAddOrEditPosition"></el-input>
        </el-form-item>
        <el-form-item label="所属部门" prop="jobDepartmentId" :rules="[{ required: true, message: '请选择所属部门', trigger: 'blur' }]">
          <el-select v-model="positionForm.jobDepartmentId" placeholder="请选择所属部门" disabled>
            <el-option :label="dept.deptName" :value="dept.id" v-for="dept in deptList" :key="dept.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资格要求" prop="jobRequirement"
        :rules="[{ required: true, message: '请输入资格要求', trigger: 'blur' }]">
          <el-input type="textarea" rows="3" v-model="positionForm.jobRequirement" placeholder="请输入资格要求" :disabled="!canAddOrEditPosition"></el-input>
        </el-form-item>
        <el-form-item label="岗位职责" prop="jobResponsibility"
        :rules="[{ required: true, message: '请输入岗位职责', trigger: 'blur' }]">
          <el-input type="textarea" rows="3" v-model="positionForm.jobResponsibility" placeholder="请输入岗位职责" :disabled="!canAddOrEditPosition"></el-input>
        </el-form-item>
        <el-form-item label="启招日期" prop="jobStartTime"
        :rules="[{ required: true, message: '请选择启招日期', trigger: 'blur' }]">
          <el-date-picker
            format="yyyy-MM-dd"
            v-model="positionForm.jobStartTime"
            :disabled="!canAddOrEditPosition"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="招聘状态" prop="jobStatus" :rules="[{ required: true, message: '请选择招聘状态', trigger: 'blur' }]">
          <el-select v-model="positionForm.jobStatus" placeholder="请选择招聘状态" :disabled="!canAddOrEditPosition">
            <el-option :label="status.value" :value="status.key" v-for="status in statusList" :key="status.key"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <div>
          <el-button size="mini" type="danger" v-if="positionForm.id && isAuth('recruitment:hrrecruitmentjob:delete')" @click="delPosition">删除</el-button>
        </div>
        <div>
          <el-button @click="dialogVisible = false" size="mini">取消</el-button>
          <el-button type="primary" @click="submitPositionForm('positionForm')" size="mini" v-if="canAddOrEditPosition">确定</el-button>
        </div>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import formUtil from '@/utils/formUtil'
import {dateUtil} from '@/utils/dateUtil'
export default {
  name: '',
  data () {
    return {
      deptList: [],
      dialogVisible: false,
      positionForm: {
        jobName: '',
        jobNum: '',
        jobDepartmentId: '',
        jobRequirement: '',
        jobResponsibility: '',
        jobStartTime: '',
        jobStatus: '',
        jobCreateTime: '',
        jobModifyTime: '',
      },
      statusList: [{value: '招聘中', key: '1'}, {value: '已招满', key: '2'}, {value: '取消', key: '9'}]
    };
  },
  components: {},
  computed: {
    canAddOrEditPosition() {
      return (this.isAuth('recruitment:hrrecruitmentjob:save') && !this.positionForm.id) || (this.isAuth('recruitment:hrrecruitmentjob:update') && this.positionForm.id)
    }
  },
  methods: {
    async getDeptList() {
      let {list} = await this.$httpN.postB('recruitment/hrrecruitmentdept/list', {limit: 999})
      this.deptList = list;
    },
    //禁止滚动
    stopScrollFun(evt) {
      evt = evt || window.event;
      if(evt.preventDefault) {
        // Firefox
        evt.preventDefault();
        evt.stopPropagation();
      } else {
        // IE
        evt.cancelBubble=true;
        evt.returnValue = false;
      }
      return false;
    },
    async init(id, dept) {
      this.getDeptList();
      formUtil.resetData(this.positionForm);
      if(this.$refs.positionForm) this.$refs.positionForm.clearValidate();
      if(id) {
        this.positionForm = await this.$httpN.postB('recruitment/hrrecruitmentjob/info', {id: id});
      } else {
        this.positionForm.jobStatus = '1';
        this.positionForm.jobDepartmentId = dept.id;
        this.positionForm.jobStartTime = dateUtil.thisDay();
      }
      this.dialogVisible = true;
    },
    submitPositionForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          if (!(await this.$httpN.postB(!this.positionForm.id ? "recruitment/hrrecruitmentjob/save" : "recruitment/hrrecruitmentjob/update", this.positionForm))) return;
          formUtil.submitSuccess("保存成功");
          this.dialogVisible = false;
          this.reload();
        }
      });
    },
    delPosition() {
      formUtil.submitConfirm('请问您是否确认删除该岗位', async ()=> {
        if(!await this.$httpN.postB('recruitment/hrrecruitmentjob/delete', [this.positionForm.id], false)) return;
        formUtil.submitSuccess('删除成功');
        this.dialogVisible = false;
        this.reload();
      })
    },
    reload() {
      this.$emit("refresh");
    }
  }
}
</script>

<style lang='scss' scoped>
.el-dialog {
  .el-form {
    max-height: 500px;
    overflow: auto;
  }
  .dialog-footer {
    display: flex;
    justify-content: space-between;
  }
}
</style>